package dongwu;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class cai {
	public static void main(String args[]) {
		WindowButton win = new WindowButton("猜数字");
	}
}

class WindowButton extends JFrame implements ActionListener {
	int number;
	JLabel hintLabel, Time, list;
	JTextField inputGuess;
	JButton buttonGetNumber, buttonEnter;

	WindowButton(String s) {
		super(s);
		addWindowListener(new WindowAdapter() { // 匿名类的实例监视窗口事件
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});
		setLayout(new FlowLayout());
		buttonGetNumber = new JButton("得到一个随机数");
		add(buttonGetNumber);
		hintLabel = new JLabel("输入你的猜测：", JLabel.CENTER);
		hintLabel.setBackground(Color.cyan);
		inputGuess = new JTextField("0", 10);
		add(hintLabel);
		add(inputGuess);
		Time = new JLabel("时间：");
		list = new JLabel("判断输赢：", JLabel.CENTER);
		list.setBackground(Color.cyan);
		add(Time);
		add(list);
		buttonEnter = new JButton("确定");
		add(buttonEnter);
		buttonEnter.addActionListener(this);
		buttonGetNumber.addActionListener(this);
		setBounds(100, 100, 150, 150);
		setVisible(true);
		validate();
	}

	long startTime = System.currentTimeMillis();

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonGetNumber) {
			number = (int) (Math.random() * 100) + 1;
			hintLabel.setText("输入你的猜测：");
		}
		// long startTime = System.currentTimeMillis();
		else if (e.getSource() == buttonEnter) {
			int guess = 0;
			try {
				guess = Integer.parseInt(inputGuess.getText());
				if (guess == number) {
					hintLabel.setText("猜对了！");
					long endTime = System.currentTimeMillis();
					long ZzTime = endTime - startTime;
					Time.setText("所用时间：" + ZzTime / 1000.0 + "秒");
					if (ZzTime / 1000.0 < 30) {
						list.setText("你赢了！");
					} else
						list.setText("你输了！");
				} else if (guess > number) {
					hintLabel.setText("猜大了！");
					inputGuess.setText(null);
				} else if (guess < number) {
					hintLabel.setText("猜小了！");
					inputGuess.setText(null);
				}

			} catch (NumberFormatException event) {
				hintLabel.setText("请输入数字字符");
			}
		}
	}
}
